import Vue from 'vue';
import VueRouter from 'vue-router';

// 导入组件
const basic1 = () => import('../pages/basic/basic1.vue')
const basic2 = () => import('../pages/basic/basic2.vue')
const basic3 = () => import('../pages/basic/basic3.vue')
const basic4 = () => import('../pages/basic/basic4.vue')
const basic5 = () => import('../pages/basic/basic5.vue')
const basic6 = () => import('../pages/basic/basic6.vue')


// 创建路由实例
const router = new VueRouter({
  path: 'history',
  routes: [
    {
      path: '/',
      component: () => import('../pages/home/home.vue'),
      children: [
        // 基础练习
        {
          path: '/basic',
          component:() => import('../pages/home/home1.vue'),//公共通用 因为嵌套了两层rout-link  所以每个一级菜单都要嵌套一下
          meta: { title: '基础练习' },
          children: [
            {
              path: '/basic1',
              component: basic1,
              meta: { title: '基础语法练习' }
            },
            { 
              path: '/basic2',
              component: basic2,
              meta: { title: '旋转的立方体' }
            },
            {
              path: '/basic3',
              component: basic3,
              meta: { title: '世界坐标辅助器和轨道控制器的添加' }
            },
            {
              path: '/basic4',
              component: basic4,
              meta: { title: '开启背景透明设置辅助网格' }
            },
            {
              path: '/basic5',
              component: basic5,
              meta: { title: '点光源' }
            },
            {
              path: '/basic6',
              component: basic6,
              meta: { title: 'GUI' }
            },
          ]
        }
        // 进阶练习
      ]
    },
  ],
});


//重写push和replace方法
//记录当前push
let push = VueRouter.prototype.push
let replace = VueRouter.prototype.replace
//后者覆盖前者
VueRouter.prototype.push = function (location, success, defeated) {
    //如果传入了成功以及失败回调就用传入的
    if (success && defeated) {
        // 使用call修改this指向为当前VueRouter实例
        push.call(this, location, success, defeated)
    } else {
        // 如果没传就设置默认的
        push.call(this, location, () => { }, () => { })
    }
}
// 重写replace
VueRouter.prototype.replace = function (location, success, defeated) {
    //如果传入了成功以及失败回调就用传入的
    if (success && defeated) {
        // 使用call修改this指向为当前VueRouter实例
        replace.call(this, location, success, defeated)
    } else {
        // 如果没传就设置默认的
        replace.call(this, location, () => { }, () => { })
    }
}

// 使用Vue Router插件
Vue.use(VueRouter);
export default router;
